﻿
//成功
const SUCCESS_CODE = 200,
    //错误
    ERROR_CODE = 400,
    //授权失败
    AUTH_FAIL = 401,
    //登陆无效
    LOGIN_INVALID_CODE = 402,
    //失败
    FAIL_DATA = 403,
    //数据为空
    NULL_DATA = 404,
    //不是会员
    NOT_MEMBER = 500,
    //会员续费通知
    MEMBER_RENEW = 501;
//接口交互统一拦截
function unifyResult(options) {
    var defaults = {
        //提示
        tipFlag: true,
        //调用方法
        func: null,
        //成功
        success: null,
        //失败
        error: null,
        //完成
        complete: null
    };
    var opt = $.extend(defaults, options);
    var res;
    try {
        var pars = new Array();
        if (arguments.length > 1) {
            for (var i = 1; i < arguments.length; i++) {
                pars.push("arguments[" + i + "]");
            }
        }
        res = eval('objectJS.' + opt.func + '(' + pars.join(",") + ')');
        if (res.ret == SUCCESS_CODE || res.ret == NULL_DATA) {
            if (opt.tipFlag) {
                //无成功回调方法，且需要提示 
                layer.msg(res.msg || "success", { icon: 1 });
            }
            if (opt.success) {
                opt.success(res);
            }
        } else if (res.ret == AUTH_FAIL) {
            layer.msg(res.msg || "权限验证失败", { icon: 0 });
        } else if (res.ret == LOGIN_INVALID_CODE) {
            layer.msg(res.msg || "请重新登陆", { icon: 0 }, function () {
                //调用退出跳转到登录页
            });
        } else {
            layer.msg(res.msg || "error", { icon: 0 });
        }

    } catch (e) {
        //失败
        if (opt.error) {
            opt.error(e);
        } else {
            $("body").prepend(e.message);
            alert(e.message);
        }
    }
    if (opt.complete) {
        //返回事件处理
        opt.complete(res);
    }
}

//定义模板
var menuHtml = `
<ul class="nav" style="position: fixed;top: 0;width: 260px;left: 0;">
    <li class="nav-item nav-profile">
        <a href="#" class="nav-link">
            <div class="nav-profile-image">
                <img id="account_headimg" src="images/headimg.png" alt="profile">
                <span class="login-status online"></span>
            </div>
            <div class="nav-profile-text d-flex flex-column">
                <span class="font-weight-bold mb-2" id="account_nicename"></span>
                <span class="text-secondary text-small" id="account_desc">快速分发平台</span>
            </div>
            <i class="mdi mdi-bookmark-check text-success nav-profile-badge"></i>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="index.html">
            <span class="menu-title">主页</span>
            <i class="mdi mdi-home menu-icon"></i>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" data-toggle="collapse" href="#ui-basic" aria-expanded="false" aria-controls="ui-basic">
            <span class="menu-title">实时新闻</span>
            <i class="menu-arrow"></i>
            <i class="mdi mdi-crosshairs-gps menu-icon"></i>
        </a>
        <div class="collapse" id="ui-basic">
            <ul class="nav flex-column sub-menu">
                <li class="nav-item"> <a class="nav-link" href="keyword.html">设置关键词</a></li>
                <li class="nav-item"> <a class="nav-link" href="articlelist.html">新闻列表</a></li>
            </ul>
        </div>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="send.html">
            <span class="menu-title">一键分发</span>
            <i class="mdi mdi-contacts menu-icon"></i>
        </a>
    </li>
</ul>`;
var footerHtml = `
<div class="d-sm-flex justify-content-center justify-content-sm-between">
    <span class="text-muted text-center text-sm-left d-block d-sm-inline-block">Copyright © 2017 <a href="http://www.9ee3.com/" target="_blank">工蜂小智</a>. All rights reserved. </span>
    <span class="float-none float-sm-right d-block mt-1 mt-sm-0 text-center">

    </span>
</div>`;


$(function () {
    $("#sidebar").html(menuHtml);
    $("#footer").html(footerHtml);

    if ($("#account_headimg").length > 0) {
        unifyResult({
            //提示
            tipFlag: false,
            func: "getLoginAccount",
            success: function (res) {
                var data = JSON.parse(res.data);
                if (data.photo != null && data.photo != "") {
                    $("#account_headimg").attr("src", data.photo);
                }
                if (data.nickname != null && data.nickname != "") {
                    $("#account_nicename").html(data.nickname);
                } else {
                    $("#account_nicename").html(data.mobile);
                }
            }
        });
    }

    var arr = location.href.split('/');
    $(".nav-item a[href='" + arr[arr.length - 1] + "']").addClass('active')
    $(".nav-item a[href='" + arr[arr.length - 1] + "']").parents(".nav-item").addClass('active');
    $(".nav-item a[href='" + arr[arr.length - 1] + "']").parents(".nav-item").find(".collapse").addClass('show');
});